package org.scboy.coobi.scan;

import java.lang.reflect.InvocationTargetException;
import java.util.Set;

import org.scboy.coobi.caches.DbStyleCache;
import org.scboy.coobi.db.DBStyle;
import org.scboy.coobi.db.context.DbStyleContext;

import cn.hutool.core.lang.ClassScanner;


/**
 * 
 * 找到不同数据库操作的模式代码类
 * 
 * @author yanghao
 *
 */
public class DbStyleScan {
	
	/**
	 * 扫描项目中的包
	 */
	public static void doScan() {
		Set<Class<?>> scanner=ClassScanner.scanPackage();
		for (Class<?> clazz : scanner) {
			
			//查找所有继承了DBStyle的类
			if(DbStyleContext.class.isAssignableFrom(clazz)&&!clazz.equals(DbStyleContext.class)) {
				try {
					//数据库方言类实例加入到缓存。
					DbStyleCache.setDbStyle((DBStyle)clazz.getDeclaredConstructor().newInstance());
				} catch (InstantiationException |IllegalAccessException | IllegalArgumentException | InvocationTargetException
						| NoSuchMethodException | SecurityException e) {
					throw new RuntimeException(e);
				}
			}
		}
	}
}
